#!/bin/bash
set -e

# Create lanchat user if it doesn't exist
if ! id -u lanchat > /dev/null 2>&1; then
    useradd --system --home-dir /opt/lanchat-server --shell /bin/false lanchat
fi

# Create data directories
mkdir -p /var/lib/lanchat/data
mkdir -p /var/lib/lanchat/uploads

# Set ownership
chown -R lanchat:lanchat /opt/lanchat-server
chown -R lanchat:lanchat /var/lib/lanchat

# Set permissions
chmod -R 755 /opt/lanchat-server
chmod -R 755 /var/lib/lanchat

# Install dependencies
cd /opt/lanchat-server
npm install --production --omit=dev

# Reload systemd
systemctl daemon-reload

# Enable service
systemctl enable lanchat-server.service

echo "LANChat Server installed successfully!"
echo "Start the service with: sudo systemctl start lanchat-server"
echo "View status with: sudo systemctl status lanchat-server"

exit 0
